package com.bear.newsset.mvp.model;

import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bear.newsset.entity.NewsDataEntity;
import com.bear.newsset.entity.NewsLableEntity;
import com.bear.newsset.entity.NewsListEntity;
import com.bear.newsset.entity.NewsParamEntity;
import com.bear.newsset.entity.ResultSubItemEntity;
import com.bear.newsset.util.DOM;
import com.bear.newsset.util.NewsItemType;
import com.ybear.ybmvp.model.YBModel;

import java.util.ArrayList;
import java.util.List;

import static com.bear.newsset.util.Constant.DOM_ID_CALL_QUERY_SITE_LIST;

public class NewsModel extends YBModel {
    private String newsToken = null;

    /**
     * 回调新闻列表
     */
    public interface CallbackNewsList {
        void result(@NonNull List<NewsListEntity> listData, @Nullable ResultSubItemEntity entity);
    }

    /**
     * 回调新闻来源列表
     */
    public interface CallbackSiteList {
        void result(@NonNull String[][] siteList);
    }

//    public interface CallbackNewsLable {
//        void result(String mainLable, String[] childLables);
//    }
    public interface CallbackNewsLable {
        void result(NewsLableEntity mainLable, List<NewsLableEntity> childLables);
    }

    /**
     * 获取当前新闻的Token
     * @return  token
     */
    public String getNewsToken() {
        return newsToken;
    }

    /**
     * 获取新闻列表
     * @param param                 筛选参数。其中token为-1表示分页，使用上一次返回的token
     * @param mCallbackNewsList     接口回调
     */
    public void getNewsList(NewsParamEntity param, @Nullable CallbackNewsList mCallbackNewsList) {
        //token为-1表示分页，当前token使用上一次返回的token
        if( "-1".equals( param.getPageToken() ) && newsToken != null ) {
            param.setPageToken( newsToken );
        }
        //查询新闻列表
        NewsRequest.queryNewsList(param, data -> {
            List<NewsListEntity> newsList = new ArrayList<>();
            List<NewsDataEntity> dataEntityList;
            if( data != null ) {
                newsToken = data.getPageToken();
                dataEntityList = data.getData();
                for(NewsDataEntity dataEntity : dataEntityList) {
                    List<String> imgUrlList = dataEntity.getImageUrls();
                    //默认为没有图片的item
                    int type = NewsItemType.TYPE_ZERO_IMG;
                    //图片数量，用于区分item
                    int imgUrlSize = imgUrlList != null ? imgUrlList.size() : 0;
                    //根据数量选择item，优先选择三张以上的，如果没有在用一张的
                    if( imgUrlSize >= 3 ) {
                        type = NewsItemType.TYPE_THREE_IMG;   //三张图片的item
                    }else if(imgUrlSize > 0) {
                        type = NewsItemType.TYPE_ONE_IMG;     //一张图片的item
                    }
                    newsList.add(new NewsListEntity(type, dataEntity));
                }
            }
            if( mCallbackNewsList != null ) mCallbackNewsList.result(newsList, data);
        });
    }

    /**
     * 获取新闻来源列表
     * @param mCallbackSiteList     接口回调
     */
    public void getSiteList(@Nullable CallbackSiteList mCallbackSiteList) {
        //查询新闻来源列表
        NewsRequest.querySiteList(entity -> {
            String[][] sites = ( sites = entity.getSiteList() ) != null ? sites : new String[][] {};
            if( mCallbackSiteList != null ) mCallbackSiteList.result( sites );
            DOM.getInstance().setResult(DOM_ID_CALL_QUERY_SITE_LIST, entity);
        });
    }

    /**
     * 查询新闻标签
     * @param position                      标签下标
     * @param mCallbackNewsLable            回调
     */
    public void getNewsLable(@IntRange(from = 0) int position,
                             @Nullable CallbackNewsLable mCallbackNewsLable) {
        //查询新闻标签列表
        getNewsLableList(map -> {
            if( position < map.size() ) {
                NewsLableEntity key = map.keySet().toArray( new NewsLableEntity[0] )[position];
                if( mCallbackNewsLable != null ) mCallbackNewsLable.result(key, map.get( key ));
                return;
            }
            if( mCallbackNewsLable != null ) {
                mCallbackNewsLable.result(new NewsLableEntity(), new ArrayList<>());
            }
        });
    }

    /**
     * 获取新闻标签列表
     * @param mCallbackNewsLableList    回调
     */
    public void getNewsLableList(NewsRequest.CallbackNewsLableList mCallbackNewsLableList) {
        //查询新闻标签列表
        NewsRequest.queryNewsLableList(mCallbackNewsLableList);
    }
}
